<template>
	<view class="xzfx-select-input">
		<view class="input-bar" @click.stop="show = true">
			<view v-if="value" class="value">{{ value }}</view>
			<view v-else class="placeholder">{{ placeholder }}</view>
			<view class="arrow xzfx_icon_arrow_right"></view>
		</view>
		<xzfx-popup ref="popup" :title="popupTitle" v-if="show" @close="show = false" @submit="submitFn">
			<date-picker v-if="show && mode === 'date'" ref="popup-date" :value="value" :fields="fields" :startDate="startDate" :endDate="endDate" />
			<date-time-picker v-if="show && mode === 'datetime'" ref="popup-datetime" :value="value" :fields="fields" :startDate="startDate" :endDate="endDate" />
			<region-picker v-if="show && mode === 'region'" ref="popup-region" :value="value" />
			<nation-picker v-if="show && mode === 'nation'" ref="popup-nation" :value="value" />
			<selector-picker v-if="show && mode === 'selector'" ref="popup-selector" :value="value" :range="range" />
			<slot></slot>
		</xzfx-popup>
	</view>
</template>

<script>
	import RegionPicker from "./components/region-picker.vue";
	import NationPicker from "./components/nation-picker.vue";
	import DatePicker from "./components/date-picker.vue";
	import DateTimePicker from "./components/date-time-picker.vue";
	import SelectorPicker from "./components/selector-picker.vue";
	export default {
		name:"xzfx-select-input",
		props: {
			value: {
				type: String,
				default: ""
			},
			mode: {
				type: String,
				default: "region"
			},
			fields: {
				type: String,
				default: "day"
			},
			startDate: {
				type: String,
				default: "1970/01/01"
			},
			endDate: {
				type: String,
				default: "2099/12/31"
			},
			title: {
				type: String,
				default: "选择器"
			},
			range: {
				type: Array,
				default: () => []
			},
			placeholder: {
				type: String,
				default: "请选择"
			}
		},
		components: {
			RegionPicker,
			NationPicker,
			DatePicker,
			SelectorPicker,
			DateTimePicker
		},
		data() {
			return {
				popupTitle: "",
				show: false
			};
		},
		created() {
			let obj = {
				date: this.title || "日期",
				datetime: this.title || "日期时间",
				region: this.title || "省市区",
				nation: this.title || "民族",
				selector: this.title
			}
			this.popupTitle = obj[this.mode] || "Popup"
		},
		methods: {
			submitFn(){
				if(this.mode !== "custom"){
					this.$emit("input", this.$refs["popup-" + this.mode].getValue());
					this.$refs.popup.closeFn()
				}else{
					if(this.value){
						this.$refs.popup.closeFn()
					}
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.xzfx-select-input{
		.input-bar{
			height: 90rpx;
			display: flex;
			align-items: center;
			text-align: right;
			font-size: 30rpx;
			line-height: 30rpx;
			.value{
				width: calc(100% - 20rpx);
				color: #333333;
				text-overflow: ellipsis;
				overflow: hidden;
				white-space: nowrap;
				text-align: end;
			}
			.placeholder{
				width: calc(100% - 32rpx);
			}
			.arrow{
				padding-top: 2rpx;
				width: 32rpx;
				font-size: 26rpx;
				color: #889eac;
				font-weight: 600;
			}
		}
	}
</style>